package login.web.dao;

import login.domain.User;
import login.web.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @Author Administrator
 * @Date 2020/6/9 14:50
 * 用户操作实现类
 **/
public class UserDaoImpl implements UserDao{
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSoutce());
    String sql="";
    @Override
    public User login(String name, String password) {
        sql="select * from user where name=? and password=?";
        /*查询出一条记录，封装成对象并返回
          当然也可能查询结果是null，会报错，所以要用try包围
         */
        try{
            /*参数1：sql
              参数2：将数据库中查询出的记录/结果，封装成对象
              参数3：给？赋值
             */
           User user = template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),name,password);
           return user;
        } catch (Exception e) {
            System.out.println( e.getMessage());
            return null;
        }


    }
}
